/*
 * 太原科技大学。
 * Copyright 2021, YCHS Information&Science Technology Co.,Ltd,
 * All rights reserved.
 */
package com.example.demo.controller;


import com.example.demo.Utils.JWTUtils;
import com.example.demo.pojo.Result;

import com.example.demo.pojo.User;
import com.example.demo.service.UserService;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author liuchao
 * @version 1.0
 * @date 2024/4/25
 */

@Slf4j
@RestController
public class userController {

    @Autowired
    private UserService userService;


    /**
     * 登录逻辑
     * @param user
     * @return
     */
    @PostMapping ("/login")
    public Result login(@RequestBody User user){

        log.info("请求登录",user);

        User u = userService.login(user);//进行用户查询验证

        /**
         * 下发token
         */
        if(u!=null){//查询到相应用户
            Map<String,Object> claims=new HashMap<>();//封装数据

            claims.put("userid",u.getUserid());
            claims.put("username",u.getUsername());
            claims.put("phone",u.getPhone());

            String jwt= JWTUtils.steJWT(claims);
            return Result.success(jwt);
        }

        return Result.error("NOT_LOGIN");
    }
    @PutMapping ("/register")
    public Result register(@RequestBody User user){
        log.info("注册请求",user);

         userService.register(user);

         return Result.success();
    }

}
